<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<link rel="stylesheet" href="../style/journal.css" type="text/css" />
<style type="text/css"><!--
.googleadsense {
	margin: 2px;
	padding: 0px;
//--></style><script src="http://www.google-analytics.com/urchin.js" type="text/javascript">
</script>
<script type="text/javascript">
_uacct = "UA-65008-1";
urchinTracker();
</script><title>Class::Date 的两个常见用途</title>
</head>
<body>
<a href="index.html">Journal</a>(2005) | <a href="../blog/"><b>Blog</b></a>(2006) | <a href="http://www.fayland.org/cgi-bin/random_link.pl">RandomLink</a> | <a href="AboutFayland.html">WhoAmI</a> | <a href="LiveBookmark.html">LiveBookmark</a> | <a href="http://www.fayland.org/">HomePage</a>
<p><&lt;Previous: <a href="Perl6_ES13.html">role Example13 { # Perl6 }</a>&nbsp;&nbsp;>>Next: <a href="050601.html">module updated</a></p>
<h1>Class::Date 的两个常见用途</h1>
<div class='content'>
<p>Category: <a href='Modules.html'>Modules</a> &nbsp; Keywords: <b>Class::Date</b></p><h3>题外话</h3>
昨天 <a href='http://blog.chunzi.org'>chunzi</a> 给我传了他改进的对身份证验证的模块，加进了地区信息和真正的生日验证等。Thanks, 非常棒。<br />
他的代码我放到这了：<a href='IdentityCard.pm.txt'>IdentityCard.pm</a>, 迟点我会对 <a href='http://search.cpan.org/perldoc?Business::CN::IdentityCard'>Business::CN::IdentityCard</a> 进行更新。（最近有三门考试 + 两门作业，有点忙）
<p>不打算讲 Class::Date 的所有用途，详细的自行参考 perldoc <a href='http://search.cpan.org/perldoc?Class::Date'>Class::Date</a> </p>

<h3>用途一：验证日期正确与否</h3>
chunzi 在模块里使用了 <a href='http://search.cpan.org/perldoc?Class::Date'>Class::Date</a> 来验证所输入身份证日期的正确性，写起来非常简单。类似的代码如下：
<pre>use Class::Date qw/:errors date/;

my $date = date '1981-02-29';
if ($date->error) { die $date->errstr; }
# 因为这伪造的日期是错误的，所以我们可以捕获得到 Range check on date or time failed</pre>
->error 有好几种类型，这里的值为 E_RANGE, 属于范围超出。

<h3>用途二：日期加减</h3>
<a href='Eplanet.html'>Eplanet</a> 有个功能为最近四天发表的后标记 New, 最近两天修改的后标记 Update.<br />
这个功能很多地方都用得着。用 <a href='http://search.cpan.org/perldoc?Class::Date'>Class::Date</a> 写这个判断非常简单。
<pre>use Class::Date qw/:errors date now/;

my $date = now; # now $date is '2005-06-01 15:00:52'
my $post_time = '2005-05-31 12:12:00';

if ($date - '4D' < $post_time) {
    print 'new';
}</pre>
非常简单。我们还有年月小时等都可以做这个加减。<br />
如果不用 Class::Date 的话要先将 $post_time 转为 1970 年到现在的秒，然后通过现在的相减小于两天的秒。比起这个复杂多了。 <a href='http://search.cpan.org'>CPAN</a> 的意义在于大家分享自己的劳动成果。（如果某个问题很普遍，去 CPAN 上找一定可以找到解决方案。）

<h3>Attention</h3>
Class::Date 似乎在 Win2k 下不支持 1970 年前的日子。比如：
<pre>use Class::Date qw/:errors date/;
my $date = date '1969-01-01';
print $date->errstr; # Range check on date or time failed</pre>
提示范围超出。后来我试了下 <a href='http://search.cpan.org/perldoc?Date::Simple'>Date::Simple</a> 似乎可以。
<pre>use Date::Simple;
my $date  = Date::Simple->new('1949-02-28');

if ($date) { print 'OK' } else { print 'UOK'; }</pre>

<h3>Enjoy</h3>
<p>Now, have a try. Thanks chunzi again. :)</p></div>
<p><&lt;Previous: <a href="Perl6_ES13.html">role Example13 { # Perl6 }</a>&nbsp;&nbsp;>>Next: <a href="050601.html">module updated</a></p>
<p><strong>Options:</strong> <a href='http://del.icio.us/post?title=Class::Date%20%E7%9A%84%E4%B8%A4%E4%B8%AA%E5%B8%B8%E8%A7%81%E7%94%A8%E9%80%94&url=http://www.fayland.org/journal/Class_Date.html'>+Del.icio.us</a></p>
<strong>Related items</strong>
<ul><li><a href='Perl6_ES13.html'>role Example13 { # Perl6 }</a> < <span class='digit'>2005-05-31 18:22:03</span> ></li><li><a href='Perl6_ES14.html'>has $.Examples14 handles 'Perl6';</a> < <span class='digit'>2005-06-02 01:38:59</span> ></li><li><a href='050519.html'>Synopsis localization</a> < <span class='digit'>2005-05-19 11:21:25</span> ></li><li><a href='Perl6_ES0.html'>@Examples[0] is Perl6</a> < <span class='digit'>2005-05-19 20:26:08</span> ></li><li><a href='Perl6_ES1.html'>given @Examples[1] when Perl6</a> < <span class='digit'>2005-05-19 22:05:56</span> ></li><li><a href='Perl6_ES2.html'>@Perl6 <== @Examples xx 2</a> < <span class='digit'>2005-05-20 18:53:54</span> ></li><li><a href='Perl6_ES3.html'>my Perl6 @Examples[3]</a> < <span class='digit'>2005-05-21 13:53:16</span> ></li><li><a href='Perl6_ES4.html'>say q:2 '@*Examples.[4] &Perl6()';</a> < <span class='digit'>2005-05-22 12:24:12</span> ></li><li><a href='Perl6_ES5.html'>sub Perl6 (*@Examples[5] is copy)</a> < <span class='digit'>2005-05-22 15:37:26</span> ></li><li><a href='Perl6_ES6.html'>multi sub Examples (6, Perl6) {...}</a> < <span class='digit'>2005-05-23 22:43:33</span> ></li><li><a href='Perl6_ES7.html'>sub infix:<(7)> ($Perl6, @Examples)</a> < <span class='digit'>2005-05-24 02:16:42</span> ></li><li><a href='Perl6_ES8.html'>let @Examples[8] = 'Perl6';</a> < <span class='digit'>2005-05-24 13:05:58</span> ></li><li><a href='Perl6_ES9.html'>Perl6 | Examples & 9 ^ junction</a> < <span class='digit'>2005-05-26 12:35:29</span> ></li><li><a href='Perl6_ES10.html'>%Examples{'IO'} = open('Perl6');</a> < <span class='digit'>2005-05-30 00:24:01</span> ></li><li><a href='Perl6_ES11.html'>class Examples11 is Perl6 { ... }</a> < <span class='digit'>2005-05-30 13:31:31</span> ></li><li><a href='Perl6_ES12.html'>$Perl6 = $class.bless(:Example<12>);</a> < <span class='digit'>2005-05-31 11:38:46</span> ></li><li><a href='Perl6_ES16.html'>my subtype Examples16 of Perl6 where /^kissu$/</a> < <span class='digit'>2005-06-04 23:59:29</span> ></li><li><a href='Perl6_ES17.html'>my enum Example17 <<:Perl(6) Today>></a> < <span class='digit'>2005-06-05 12:14:08</span> ></li><li><a href='Perl6_ES18.html'>my $queen; rule me {Perl6|Examples18}</a> < <span class='digit'>2005-06-06 12:26:32</span> ></li><li><a href='Perl6_ES19.html'>m:nth(19)/Perl6Examples/</a> < <span class='digit'>2005-06-06 22:02:01</span> ></li><li><a href='Perl6_Rules.html'>用 Perl6::Rules 来写 Formatter</a> < <span class='digit'>2005-09-27 10:02:45</span> ></li></ul>
Created on <span class="digit">2005-06-01 15:02:47</span>, Last modified on <span class="digit">2005-11-08 01:57:43</span><br />
Copyright 2004-2005 All Rights Reserved. Powered by <a href="Eplanet.html">Eplanet</a> && <a href='http://catalyst.perl.org'>Catalyst</a> 5.62.
</body>
</html>